package com.ykdl.member.kid.gears;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.adapters.DoctorTalkChildListAdapter;
import com.ykdl.member.kid.adapters.FreakyChatChildListAdapter;
import com.ykdl.member.kid.adapters.HeatImageListAdpater;
import com.ykdl.member.kid.beans.DoctorTalkChildListBean;
import com.ykdl.member.kid.beans.FreakyChatChildListBean;
import com.ykdl.member.kid.beans.HeatImageListBean;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.gears.DoctorTalkChildListActivity.DoctorTalkChildListTag;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.util.RequestAsAccessTokenUtil;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

public class HeatImageListActivity extends AbsFragmentActivity {
	
	private HeatImageListBean heatImageListBean;
	private HeatImageListAdpater adapter;
	private int gridview_item_image_height;
	private HttpBaseRequest request;
	
	
	@Override
	protected void getRequestData() {
		showProgress(this);
		final String  url = KidConfig.BASE_TEST_SERVER + "topics/hot";
		HttpParameters   urlParams = new HttpParameters();
		request = Wxxr.getInstance().getRequest(url, urlParams);
		TaskManager.startHttpRequest(request, new HeatImageListTag(), HeatImageListBean.class);
	}
	
	class HeatImageListTag implements ITag{
		@Override
		public void refreshUI(Object t, DataState ds) {
			heatImageListBean = (HeatImageListBean)t;
			if(heatImageListBean.isExpired()){
				RequestAsAccessTokenUtil.getInstance(mContext).refreshTokenRequest(request, this, HeatImageListBean.class);
			}else{
				showdata();
				dismissProgress();
			}
		}
		@Override
		public boolean refreshError(DataParseError parseError) {
			//下拉加载更多时，请求数据错误时，取消正在加载状态
			removeFootRefreshView();
			dismissProgress();
			return false;
		}

		@Override
		public void refreshCancel() {
		}
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		/**单个话题，进入话题终端页*/
		final TopicBean topic = this.heatImageListBean.getTopics().get(arg2);
		Intent intent =new Intent(this,TopicAndResultActivity.class);
		intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getTopic_id());
		intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getSource_id());
		this.startActivity(intent);
	}

	@Override
	protected void setTitle() {
		this.tv_title.setText("");
		this.tv_title.setText("热图榜");

	}

	@Override
	protected void matchGridView() {
		
		final int screenWidth = MobileUtils.getScreenWidthIntPx();
		final int gridview_width = (screenWidth - MobileUtils.dpToPx(12));
		gridview_item_width = gridview_width / 3; 
		gridview_item_image_height = (int)((gridview_item_width- MobileUtils.dpToPx(12)) / 0.97);
		
		LayoutParams  params = (LayoutParams) mGridView.getLayoutParams();
		params.width = gridview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		mGridView.setLayoutParams(params);
		
		
		mGridView.setHorizontalSpacing(MobileUtils.dpToPx(6));
		mGridView.setVerticalSpacing(MobileUtils.dpToPx(6));
		mGridView.setColumnWidth(gridview_item_width);
		mGridView.setCacheColorHint(getResources().getColor(R.color.touming));
		mGridView.setNumColumns(3);
	}

	@Override
	protected void showdata() {
		
		//加载更多数据完成
		removeFootRefreshView();
		if(heatImageListBean.getNext_cursor() == heatImageListBean.getTotal_number())
		{
			mPullToRefreshView.setEnablePullLoadMoreDataStatus(false);
		}
		if(this.heatImageListBean.getTopics() != null && this.heatImageListBean.getTopics().size() > 0){
			if(adapter == null){
				adapter = new HeatImageListAdpater(this.mContext, bitmapUtil, gridview_item_image_height);
				adapter.setList(this.heatImageListBean.getTopics());
				mGridView.setAdapter(adapter);
			}
			this.cursor = heatImageListBean.getNext_cursor();
			this.total_count = heatImageListBean.getTotal_number();
			adapter.notifyDataSetChanged();
		}else{
			
			if(isFootRefresh){
				Toast.makeText(this, "没有更多数据！", 0).show();
			}else{
				Toast.makeText(this, "没有数据哦！", 0).show();
			}
		}

	}

}
